# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2023-01-16
desc: 
sample: 
"""
import re

import requests

def test_reg2():
    str = 'a123ba456bab'
    print(re.findall('a(.+)b', str)) #贪婪 最大提取
    print(re.findall('a(.+?)b', str)) #非贪婪，最小
    print(re.findall('a(.*?)b', str))
    print(re.findall('a(.*)b', str))

def test_reg():
    # 匹配字符串，提取字符串
    print(re.match('www', 'www.runoob.com'))
    print(re.match('com', 'www.runoob.com'))
    print(re.search('com', 'www.runoob.com'))

    r = r"\d+"
    result = re.search(r, "a123test")
    print(result)

    result2 = re.findall(r, "a123test")
    print(result2)

    xml = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope ' \
          'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' \
          ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' \
          ' xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>' \
          '<getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">' \
          '<getMobileCodeInfoResult>18810982234：北京 北京 北京移动全球通卡</getMobileCodeInfoResult>' \
          '</getMobileCodeInfoResponse></soap:Body></soap:Envelope>'
    # 分组技巧()
    r = '<getMobileCodeInfoResult>(.+)</getMobileCodeInfoResult>'
    result3 = re.findall(r, xml) #list(list)
    print(result3)

    print("search--")
    # p = re.compile(r)
    # search = p.search(xml)
    search = re.search(r, xml)
    if search:
        print(search.group())
        print(search.group(1))

def test_soap():
    url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"
    data = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope ' \
           'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"' \
           ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' \
           '<soap:Body><getMobileCodeInfo xmlns="http://WebXml.com.cn/">' \
           '<mobileCode>18810982234</mobileCode>' \
           '<userID></userID>' \
           '</getMobileCodeInfo>' \
           '</soap:Body></soap:Envelope>'
    xml_header = {'Content-type': 'text/xml'}
    response = requests.post(url, data=data,headers=xml_header)
    print(f"是否成功{response.status_code}")
    print(f"返回结果{response.text}")
    #提取 xml html
    r = '<getMobileCodeInfoResult>(.+)</getMobileCodeInfoResult>'
    result = re.findall(r, response.text)
    print(result[0])

if __name__ == '__main__':
    #test_soap()
    #reg_test()
    #reg_test2()
    pass

